﻿/*    
    OpenMheg - A free MHEG-5 library
    Copyright (C) 2008 Dharmesh Malam

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Text;
using Exceptions;
using Core.Model;
using ASN1Decoder.Builders.Nodes;
using Exceptions;
using Core.Model.Groups;
using Core.Model.Ingredients.Links;

namespace ASN1Decoder.Builders
{
    internal class LinkBuilder
    {
        internal static Link Build(TaggedNode node, Group group)
        {
            var link = new Link();
            IngredientBuilder.Build(node, link, group);

            TaggedNode e;

            if ((e = node[ASN1ContextCodes.C_LINK_CONDITION]) != null)
                link.LinkCondition = BuildLinkCondition(e, group);
            else
                throw new OpenMhegException("Link Condition not Optional");

            if ((e = node[ASN1ContextCodes.C_LINK_EFFECT]) != null)
                link.LinkEffect = ActionBuilder.Build(e, group);
            else
                throw new OpenMhegException("Link Effect not Optional");

            return link;
        }

        private static LinkCondition BuildLinkCondition(TaggedNode node, Group group)
        {
            var linkCondition = new LinkCondition();

            TaggedNode e;

            if (node.Size < 2)
                throw new OpenMhegException("Missing data");
            //if ((e = node[ASN1ContextCodes.P_EVENT_SOURCE]) != null)
                linkCondition.EventSource = ObjectRefBuilder.Build(node.First, group);
            //else
              //  throw new OpenMhegException("Event Source not Optional");

            //if ((e = node[ASN1ContextCodes.P_EVENT_TYPE]) != null)
                linkCondition.EventType =Core.Model.Events.Event.getEvent(node.Second.EnumValue);
            //else
              //  throw new OpenMhegException("Event Type not Optional");

            //if ((e = node[ASN1ContextCodes.P_EVENT_DATA]) != null)
            if (node.Size > 2)
            {
                ASN1Node t = node.Third;

                if (t is BoolNode)
                    linkCondition.EventData = t.BoolValue;
                else if ( t is IntNode)
                    linkCondition.EventData = t.IntValue;
                else if ( t is OctetStringNode)
                    linkCondition.EventData = t.StringValue;
                else
                    throw new OpenMhegException("Could not decode linkCondition.Eventdata");

            }

            return linkCondition;
        }
    }
}
